package app.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.app.R;
import androidx.recyclerview.widget.RecyclerView;

public class SampleListView extends RecyclerView
{
    int mItemCount;
    int mItemLayout;

    public SampleListView(Context context){
        super(context);
        init(context, null);
    }

    public SampleListView(Context context, AttributeSet attrs){
        super(context, attrs);
        init(context, attrs);
    }

    public SampleListView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SampleListView);
        mItemCount = ta.getInteger(R.styleable.SampleListView_itemCount, 0);
        mItemLayout = ta.getResourceId(R.styleable.SampleListView_itemLayout, 0);
        ta.recycle();
        setAdapter(new Adapter()
        {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup pa, int type){
                int layout = mItemLayout;
                if(layout == 0) layout = android.R.layout.simple_list_item_1;
                View v = LayoutInflater.from(getContext()).inflate(layout, pa, false);
                return new ViewHolder(v){};
            }

            @Override
            public void onBindViewHolder(ViewHolder h, int p){
                View view = h.itemView;
                if(!(view instanceof TextView)){
                    view = view.findViewById(android.R.id.text1);
                }
                if(view instanceof TextView){
                    ((TextView)view).setText((p + 1) + "/" + mItemCount);
                }
            }

            @Override
            public int getItemCount(){
                return mItemCount;
            }
        });
    }

}
